package edu.iscas.appcheckclient.model.actions;

import edu.iscas.appcheckclient.model.Selector;

public class Perform extends ParamAction {
    public static final String jsonName = "perform";
    protected Selector selector;
    protected long time;
    protected Perform.Type type;

    public enum Type {
        Click, LoadInAdapterView, LongClick, ScrollAdapterView, TypeViewSelected, Type
    }

    public Perform(Type type, Selector selector, long time) {
        this.name = "perform";
        this.selector = selector;
        this.type = type;
        this.time = time;
    }

    @Override
    public void setStatement(String statement) {
        this.statement = statement;
    }

    @Override
    public String brewStatement(boolean paramBoolean){
        String str = "";
        switch (type){
            case Click:
                str = "click()";
                return "onView(" + this.selector.brewStatement(paramBoolean) + ").perform(" + str + ")";
            case LoadInAdapterView:
                return "onData(Matchers.anything()).inAdapterView(" + this.selector.brewStatement(paramBoolean) + ").atPosition(" + getIntParam("position") + ").loadData()";
            case LongClick:
                str = "longClick()";
                return "onView(" + this.selector.brewStatement(paramBoolean) + ").perform(" + str + ")";
            case ScrollAdapterView:
                return "onData(anything()).inAdapterView(" + this.selector.brewStatement(paramBoolean) + ").atPosition(" + getIntParam("position") + ").check(matches(isDisplayed()))";
            case TypeViewSelected:
                if (getStringParam("classType").equals("android.widget.AbsSpinner")){
                    if (!getStringParam("text").isEmpty()) {
                        return "onData(is((Object)\"" + getStringParam("text").replace("[", "").replace("]", "") + "\")).inAdapterView(" + this.selector.brewStatement(paramBoolean) + ").perform(click())";
                    }
                    return "onData(anything()).inAdapterView(" + this.selector.brewStatement(paramBoolean) + ").atPosition(" + getIntParam("position") + ").perform(click())";
                }
            case Type:
                if (getBoolParam("is_password")){
                    str = "clearText(), typeTextIntoFocusedView(password)";
                    return "onView(" + this.selector.brewStatement(paramBoolean) + ").perform(" + str + ")";
                }else{
                    str = "clearText(), typeTextIntoFocusedView(\"" + getStringParam("text") + "\")";
                    return "onView(" + this.selector.brewStatement(paramBoolean) + ").perform(" + str + ")";
                }
        }
        return str;
    }

    public long getTime() {
        return time;
    }

    public Type getType() {
        return type;
    }

    @Override
    public Selector getSelector() {
        return this.selector;
    }

    @Override
    public String getTypeString() {
        return "perform " + getType().toString();
    }

    @Override
    public String getValue() {
        if (getSelector() != null) {
            return getSelector().getValue();
        }
        return "";
    }
}
